package cn.com.zsc.income;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.zsc.util.ConstValue;

import com.jarp.db.DAO;

/**
 * 收入报表
 * @author 杨景
 *
 */
public class IncomeMessages {
	/**
	 * 获取收入信息
	 * 
	 * @param start
	 * @param limit
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getIncome(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
		String workID = jsobject.getString("workID");
		String eName = jsobject.getString("eName");
		String startDate = jsobject.getString("startDate");
		String endDate = jsobject.getString("endDate");
		String attractions = jsobject.getString("attractions");
		String incomeType = jsobject.getString("incomeType");
		
		String sql = "select " +
							"B.CL_NAME AS SHOW_INCOMETYPE, " +
							"D.CL_NAME AS SHOW_DISCOUNT, " +
							"A.* " +
					 "from ic_manage A " +
					 		"left join cm_commlist B on A.INCOMETYPE=B.CL_CODE " +
					 		"left join cm_commlist D on A.DISCOUNT=D.CL_CODE where 1=1 ";
		
		if (!"".equals(workID)) {
            sql += " and A.WORKID like '%" + workID + "%'";
		}
		
		if (!"".equals(eName)) {
            sql += " and A.ENAME like '%" + eName + "%'";
		}
		
		if (!"".equals(startDate)) {
            sql += " and A.STARTTIME>='" + startDate + "'";
		}
		
		if (!"".equals(endDate)) {
            sql += " and A.STARTTIME<='" + endDate + "'";
		}
		
		if (!"".equals(attractions)) {
			String[] attractionArray = attractions.split(",");
			
			for (int i=0; i<attractionArray.length; i++) {
				sql += " and A.ATTRACTIONS like '%"+attractionArray[i]+"%' ";	
			}
		}
		
		if (!"".equals(incomeType)) {
            sql += " and A.INCOMETYPE = '" + incomeType + "'";
		}
		
		sql += " order by A.STARTTIME desc";
		
		DAO dao = new DAO();
	
		String resultStr = ConstValue.EMPTY_RECORD_NOT_PAGEING;
		
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);

			if (v != null && v.size() > 0) {
				for (int i=0; i<v.size(); i++) {
					v.get(i).put("SHOW_ATTRACTIONS", getAttractionsByIds(v.get(i).get("ATTRACTIONS")));
					v.get(i).put("SHOW_STARTTIME", v.get(i).get("STARTTIME").substring(0,10));
					v.get(i).put("SHOW_STARLEVEL", getEmpStarLevelByGuideID(v.get(i).get("GUIDEID")));
				}
				
				JSONArray json = JSONArray.fromObject(v);
				
				resultStr = json.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultStr;
	}
	
	/**
	 * 获取每日业绩核对
	 * 
	 * @param start
	 * @param limit
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getDayAchievement(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
		String workID = jsobject.getString("workID");
		String eName = jsobject.getString("eName");
		String guideDate = jsobject.getString("guideDate");
		
		String sql = "select " +
						"B.CL_NAME AS SHOW_STARLEVEL, " +
						"A.* " +
					 "from ic_day_business A " +
					 	"left join cm_commlist B on A.STAR = B.CL_CODE where 1=1";
		
		if (!"".equals(workID)) {
            sql += " and A.GUIDEID like '%" + workID + "%'";
		}
		
		if (!"".equals(eName)) {
            sql += " and A.GUIDENAME like '%" + eName + "%'";
		}
		
		if (!"".equals(guideDate)) {
            sql += " and A.DATE='" + guideDate + "'";
		}
		
		sql += " order by A.DATE desc";
		
		DAO dao = new DAO();
	
		String resultStr = ConstValue.EMPTY_RECORD_PAGEING;
		
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);

			if (v != null && v.size() > 0) {
				for (int i=0; i<v.size(); i++) {
					v.get(i).put("SIGNATURE", "");
				}
				
				JSONArray json = JSONArray.fromObject(v);
				
				resultStr = json.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultStr;
	}
	
	/**
	 * 根据观光点ID查询观光点
	 * @param attractionsIds
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getAttractionsByIds(String attractionsIds) {
		String sql = "select CL_NAME from cm_commlist where CL_CODE in (" + attractionsIds + ")";
		
		DAO dao = new DAO();
		
		String attractions = "";
		
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);
			
			if (v != null && v.size() > 0) {
				for (int i=0; i<v.size(); i++) {
					attractions += v.get(i).get("CL_NAME") + ",";
				}
				
				attractions = attractions.substring(0, attractions.length()-1);
			}
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
		return attractions;
	}
	
	/**
	 * 获取员工星级
	 * @param jso
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getEmpStarLevelByGuideID(String guideID) {
		String sql = "select A.EID, B.CL_NAME  from hr_employee A left join cm_commlist B on  " +
				"A.STARLEVEL=B.CL_CODE where WORKID = '" + guideID + "'";
		
		DAO dao = new DAO();
		
		String starLevel = "";
			
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);
			
			if(v!=null && v.size()>0) {
				starLevel = v.get(0).get("CL_NAME");
			}
		} catch (ServletException e) {
			e.printStackTrace();
			return "数据库操作失败";
		}
		
		return starLevel;
	}
}
